package com.qfedu.mtl.util;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Menus;
import com.qfedu.mtl.util.page.Pager;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpUtil {

    /**
     * 自动从请求中把 全部参数获取出来，然后返回对象
     * @param req
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> T  request2Bean(HttpServletRequest req , Class<T> cls ){
        // 全部数据
        Map<String, String[]> map = req.getParameterMap();
        // 把这些数据映射到对象属性。
        T obj = null;
        try {
            //创建对象
            obj = cls.newInstance();
            //遍历所用数据
            for (String name : map.keySet()) {
                try {

                    //根据前端参数查找同名属性，如果找不到则放弃。
                    Field fd = cls.getDeclaredField(name);
                    String[] value = map.get(name);
                    //设置访问权限
                    fd.setAccessible(true);
                    // 1. 类型问题
                    if (fd.getType().isArray()) {
                        fd.set(  obj, value  );
                    }else{
                        if( fd.getType()== Date.class){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = sdf.parse(value[0]);
                            fd.set(  obj,date);
                        }
                        if( fd.getType()== Integer.class){
                            if(!StrUtil.isNullOrEmpty(value[0]) ){
                                fd.set(  obj,Integer.valueOf(value[0]));
                            }
                        }
                        if( fd.getType()== Long.class){
                            if(!StrUtil.isNullOrEmpty(value[0]) ){
                                fd.set(  obj,Long.valueOf(value[0]));
                            }

                        }
                        if( fd.getType()== Double.class){
                            if(!StrUtil.isNullOrEmpty(value[0]) ) {
                                fd.set(obj, Double.valueOf(value[0]));
                            }
                        }
                        if( fd.getType()== String.class){
                            if(!StrUtil.isNullOrEmpty(value[0]) ) {
                                fd.set(obj, value[0]);
                            }
                        }
                    }
                } catch (NoSuchFieldException e) {

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static void responseSuccess(List<Menus> treeList, HttpServletResponse response) throws IOException {
        Map<String,Object> vo = new HashMap<>();
        vo.put("code",0);
        vo.put("msg","操作成功");
        vo.put("data",treeList);
        response.getWriter().println(JSON.toJSONString(vo));
    }

    public static <T> void responsePage(Pager<T> pager,  HttpServletResponse response) throws IOException {
        Map<String,Object> vo = new HashMap<>();
        vo.put("code",0);
        vo.put("msg","操作成功");
        vo.put("data",pager);
        response.getWriter().println(JSON.toJSONString(vo));
    }

    public static void response(Object o, int code, String msg, HttpServletResponse response) throws IOException {
        Map<String,Object> vo = new HashMap<>();
        vo.put("code",code);
        vo.put("msg",msg);
        vo.put("data",o);
        response.getWriter().println(JSON.toJSONString(vo));

    }

    public static void response(Object data,Integer code,String msg,HttpServletResponse response) throws IOException {
        String s = JSON.toJSONString(data);
        Map<String,Object> vo = new HashMap<>();
        vo.put("code",0);
        vo.put("msg",msg);
        vo.put("data",data);
        String s1 = JSON.toJSONString(vo);
        response.getWriter().println(s1);
    }

    public static void responseSuccess(Object data,HttpServletResponse response) throws IOException {
        Map<String,Object> vo = new HashMap<>();
        vo.put("code",0);
        vo.put("msg","成功");
        vo.put("data",data);
        String s1 = JSON.toJSONString(vo);
        response.getWriter().println(s1);
    }
}


